package com.cskaoyan.controller.mall;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.bo.mall.OrderShipBo;
import com.cskaoyan.bean.vo.mall.OrderDetailVo;
import com.cskaoyan.bean.vo.mall.OrderListVo;
import com.cskaoyan.service.mall.OrderService;
import com.cskaoyan.util.common.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Map;

/**
 * @Author Ajun de BBC
 * @Date 2022/7/16 18:39
 * @Version 1.5
 */
@RestController
@RequestMapping("admin/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @RequestMapping("list")
    public BaseRespVo orderList(Integer page, Integer limit, Short[] orderStatusArray, String sort, String order, Integer orderId, String start, String end, Integer userId, String orderSn) throws ParseException {
        OrderListVo orderListVo = orderService.orderList(page,limit,orderStatusArray,sort,order,orderId,start,end,userId,orderSn);
        return BaseRespVo.ok(orderListVo,"成功",0);
    }

    @RequestMapping("detail")
    public BaseRespVo orderDetail(Integer id){
        OrderDetailVo orderDetailVo = orderService.orderDetail(id);
        return BaseRespVo.ok(orderDetailVo,"成功",0);
    }

    /**
     *@Description : 修改订单发货时，快递公司与编号必须同时都有数据
     *@Param :
     *@Return :
     */
    @RequestMapping("ship")
    public BaseRespVo orderShip(@RequestBody OrderShipBo orderShipBo){
        if((orderShipBo.getShipSn() == null || orderShipBo.getShipSn().length() <= 0) || (orderShipBo.getShipChannel() == null || orderShipBo.getShipChannel().length() <= 0 )){
            return BaseRespVo.ok(null,"参数不对",401);
        }
        orderService.orderShip(orderShipBo);
        return BaseRespVo.ok(null,"成功",0);
    }

    @RequestMapping("delete")
    public BaseRespVo orderDelete(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");
        int i = orderService.orderDelete(orderId);
        if(i == 623){
            return BaseRespVo.ok(null,"订单不能删除",623);
        }
        return BaseRespVo.ok(null,"成功",0);
    }

    @RequestMapping("refund")
    public BaseRespVo orderRefund(@RequestBody Map map){
        Integer orderId = ((Integer) map.get("orderId"));
        Integer refundMoney = ((Integer) map.get("refundMoney"));
        orderService.orderRefund(orderId,refundMoney);
        return BaseRespVo.ok(null,"成功",0);
    }


    /**
     *@Description : 订单发货的快递公司显示
     *@Param :
     *@Return :
     */
    @RequestMapping("channel")
    public String orderChannel(){
        return Constant.ORDER_CHANNEL;
    }

    @RequestMapping("reply")
    public BaseRespVo orderReply(@RequestBody Map map){
        Integer commentId = (Integer)map.get("commentId");
        String content = (String)map.get("content");
        Integer i = orderService.orderReply(commentId,content);
        if( i == null){
            BaseRespVo.ok(null,"订单商品已回复",622);
        }
        return BaseRespVo.ok(null,"成功",0);
    }


}
